package com.stylefeng.guns.modular.owBackstageMgr.mmMgr.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.owBackstageMgr.newsMgr.service.INewsService;
import com.stylefeng.guns.modular.system.model.News;
import com.stylefeng.guns.modular.system.warpper.MemberMienWarpper;
import com.stylefeng.guns.modular.system.warpper.NewsWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.MemberMien;
import com.stylefeng.guns.modular.owBackstageMgr.mmMgr.service.IMemberMienService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员风采管理控制器
 *
 * @author fengshuonan
 * @Date 2018-07-16 14:11:24
 */
@Controller
@RequestMapping("/memberMien")
public class MemberMienController extends BaseController {

    private String PREFIX = "/mmMgr/memberMien/";

    @Autowired
    private IMemberMienService memberMienService;

    @Autowired
    private INewsService newsService;

    /**
     * 跳转到会员风采管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "memberMien.html";
    }

    /**
     * 跳转到添加会员风采管理
     */
    @RequestMapping("/memberMien_add")
    public String memberMienAdd() {
        return PREFIX + "memberMien_add.html";
    }

    /**
     * 跳转到修改会员风采管理
     */
    @RequestMapping("/memberMien_update/{memberMienId}")
    public String memberMienUpdate(@PathVariable Integer memberMienId, Model model) {
        MemberMien memberMien = memberMienService.selectById(memberMienId);
        model.addAttribute("mm",memberMien);
        LogObjectHolder.me().set(memberMien);
        return PREFIX + "memberMien_edit.html";
    }

    /**
     * 获取会员风采管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String condition) {
        Page<MemberMien> page = new PageFactory<MemberMien>().defaultPage();
        List<Map<String, Object>> result = memberMienService.getMMList(page, beginTime, endTime, condition, page.getOrderByField(), true);
        page.setRecords((List<MemberMien>) new MemberMienWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增会员风采管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(MemberMien memberMien) {
        if (ToolUtil.isOneEmpty(memberMien, memberMien.getName())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        // 新闻创建时间是后台设置的时候，更新时间与之相同;非后台设置，则两者都为当前时间
        if (memberMien.getCreateTime() != null){
            memberMien.setUpdateTime(memberMien.getCreateTime());
        }else {
            memberMien.setCreateTime(new Date());
            memberMien.setUpdateTime(new Date());
        }
        memberMienService.insert(memberMien);
        return SUCCESS_TIP;
    }

    /**
     * 删除会员风采管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long memberMienId) {

        if (memberMienId != null){
            MemberMien memberMien = memberMienService.selectById(memberMienId);
            if (memberMien != null){
                memberMien.setUpdateTime(new Date());
                memberMien.setIsDel(1);// 表示删除状态
                memberMien.updateById();
                return SUCCESS_TIP;
            }else {
                return new ErrorTip(-1,"找不到需要删除的记录");
            }
        }
        return new ErrorTip(-1,"参数错误");
    }

    /**
     * 修改会员风采管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(MemberMien memberMien) {

        if (ToolUtil.isNotEmpty(memberMien)) {
            memberMien.setUpdateTime(new Date());
            memberMienService.updateById(memberMien);
            return SUCCESS_TIP;
        }
        return new ErrorTip(-1,"传入的需要更新的对象为空~");
    }

    /**
     * 会员风采管理详情
     */
    @RequestMapping(value = "/detail/{memberMienId}")
    @ResponseBody
    public Object detail(@PathVariable("memberMienId") Integer memberMienId) {
        return memberMienService.selectById(memberMienId);
    }

    /**
     * 获取所有的会员风采相关的新闻数据
     */
    @RequestMapping(value = "/getAllMMNews")
    @ResponseBody
    public String getAllMMNews(){
        List<News> mmNews = newsService.getAllMMNews(null);
        return JSON.toJSONString(mmNews);
    }
}
